/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * consultarLote.java
 *
 * Created on 01-may-2011, 17:49:01
 */

package interfaces.farmacia;
import Controlador.Controlador;
import Definiciones.Definiciones.Estado;
import GestorFarmacia.Lote;
import GestorFarmacia.Medicamento;
import clinica.ClinicaView;
import clinica.Interfaces;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.jdesktop.application.Action;

/**
 *
 * @author frpp
 */
public class consultarLote extends javax.swing.JPanel {

    private TableModel modelo;
    private ClinicaView app;
    private Interfaces anterior;
    private Integer _id_medicamento;
    ArrayList<Lote> _lote = new ArrayList<Lote>();


    /** Creates new form consultarLote */
    public consultarLote(ClinicaView a) {
        app = a;
        initComponents();

        jTable3.setRowHeight(32);
        jTable3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

    public void rellenaDatos(Integer id_medicamento, Interfaces a) throws IOException, ClassNotFoundException, SQLException, ParseException{
        DateFormat fecha_formato = new SimpleDateFormat("dd/MM/yyyy");
        _lote.clear();
        anterior = a;
        _id_medicamento = id_medicamento;

        Lote[] lote = Lote.get(id_medicamento, null, null, null, null, null);
        if (lote != null) {
            for(int i =0; i< lote.length; i++)
               _lote.add(lote[i]);
        }
      limpiarTabla();
        /* Colocamos en la tabla */
        for(Lote l:_lote) {
            String estado = "Disponible";
            if(l.getEstadoLote() == Definiciones.Definiciones.Estado.DESECHADO) estado = "Desechado";
            if(l.getEstadoLote() == Definiciones.Definiciones.Estado.TERMINADO) estado = "Terminado";
//falta poenr el nombre de l medicamento
            String [] valores = {" "+l.getIdLote()," "+Medicamento.get(l.getIdMedicamento()).getNombreMedicamento()," "+fecha_formato.format(l.getFechaCaducidadLote()),
                                 ""+estado};
            ((DefaultTableModel)jTable3.getModel()).addRow(valores);
        }


    }


    private void limpiarTabla() {
        while(((DefaultTableModel)jTable3.getModel()).getRowCount()>0)
            ((DefaultTableModel)jTable3.getModel()).removeRow(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Lotes encontrados", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Grande", 0, 18))); // NOI18N
        jPanel1.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        jPanel1.setMaximumSize(new java.awt.Dimension(780, 470));
        jPanel1.setMinimumSize(new java.awt.Dimension(780, 470));
        jPanel1.setName("jPanel1"); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Acciones", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Grande", 0, 18))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(consultarLote.class, this);
        jButton5.setAction(actionMap.get("verLote")); // NOI18N
        jButton5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(consultarLote.class);
        jButton5.setIcon(resourceMap.getIcon("jButton5.icon")); // NOI18N
        jButton5.setText("Ver lote");
        jButton5.setToolTipText("");
        jButton5.setName("jButton5"); // NOI18N
        jButton5.setPreferredSize(new java.awt.Dimension(0, 0));

        jButton6.setAction(actionMap.get("deshecharLote")); // NOI18N
        jButton6.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jButton6.setIcon(resourceMap.getIcon("jButton6.icon")); // NOI18N
        jButton6.setText("Deshechar lote");
        jButton6.setToolTipText("");
        jButton6.setName("jButton6"); // NOI18N
        jButton6.setPreferredSize(new java.awt.Dimension(0, 0));

        jButton7.setAction(actionMap.get("modificarLote")); // NOI18N
        jButton7.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jButton7.setIcon(resourceMap.getIcon("jButton7.icon")); // NOI18N
        jButton7.setText("Modificar lote");
        jButton7.setToolTipText("");
        jButton7.setName("jButton7"); // NOI18N
        jButton7.setPreferredSize(new java.awt.Dimension(0, 0));

        jPanel3.setName("jPanel3"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 19, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 83, Short.MAX_VALUE)
        );

        jPanel4.setName("jPanel4"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 16, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 83, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jButton5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(23, 23, 23)
                .add(jButton7, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(jButton6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jButton5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                    .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jButton6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                    .add(jButton7, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE))
                .addContainerGap())
        );

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable3.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        jTable3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código Lote", "Medicamento", "Fecha Cad.", "Estado"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable3.setName("jTable3"); // NOI18N
        jTable3.setUpdateSelectionOnSort(false);
        jScrollPane1.setViewportView(jTable3);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 696, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jButton2.setAction(actionMap.get("aceptar")); // NOI18N
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setLabel(resourceMap.getString("jButton2.label")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jButton3.setAction(actionMap.get("cancelar")); // NOI18N
        jButton3.setIcon(resourceMap.getIcon("jButton3.icon")); // NOI18N
        jButton3.setLabel(resourceMap.getString("jButton2.label")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 748, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jButton3)
                        .add(18, 18, 18)
                        .add(jButton2)))
                .add(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(28, 28, 28)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 341, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jButton2)
                    .add(jButton3))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents



    @Action
    public void aceptar(){
        _lote.clear();
        app.volver(Interfaces.PRINCIPAL_FARMACEUTICO);
    }

    @Action
    public void cancelar() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException {
        _lote.clear();
        app.volver(anterior);
    }

    
    @Action
    public void verLote() throws SQLException, NoSuchAlgorithmException, ParseException, IOException, ClassNotFoundException {
        int row = jTable3.getSelectedRow();
        app.abrirVerLote(_lote.get(row),Interfaces.CONSULTAR_LOTE);
    }

    @Action
    public void modificarLote() throws ParseException, SQLException, IOException, ClassNotFoundException, NoSuchAlgorithmException{
        int row = jTable3.getSelectedRow();
        app.abrirModificarLote(_lote.get(row),Interfaces.BUSCAR_MEDICAMENTO_2);//
        
    }

 //Tieen que vovler a la interfaz de consultarLote.
    @Action
    public void deshecharLote() throws IOException, ClassNotFoundException, SQLException, ParseException {
        int row = jTable3.getSelectedRow();
        if (row >= 0) {
            if ( _lote.get(row).getEstadoLote().toString().equals("DESECHADO")) {
                app.abrirMensajeError("El lote seleccionado ya se encuentra desechado.", Interfaces.CONSULTAR_LOTE);
            } else {
                Lote.delete(_lote.get(row));//la borramos de la BD
                _lote.remove(row);
                app.abrirMensajeExito("Lote desechado correctamente.", Interfaces.CONSULTAR_LOTE);
                rellenaDatos(_id_medicamento, anterior);
            }
        } else {
            app.abrirMensajeError("No seleccionó ningún lote para desecharlo.", Interfaces.CONSULTAR_LOTE);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable3;
    // End of variables declaration//GEN-END:variables

}
